package com.general.seckill.orderservice;

import com.general.seckill.common.IdWorker;
import com.general.seckill.common.Result;
import com.general.seckill.orderservice.entity.UserOrder;
import com.general.seckill.orderservice.entity.UserOrderExample;
import com.general.seckill.orderservice.mapper.OrderMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description: OrderService
 * @author: general
 * @version: 1.0
 * @create: 2022-04-30 22:32
 **/
@Service
public class OrderService {

    @Resource
    private OrderMapper orderMapper;


    public Result<?> createOrder(SecKillEntity entity) {
        UserOrder userOrder = new UserOrder();
        userOrder.setUserId(entity.getUserId());
        userOrder.setProductId(entity.getProductId());
        userOrder.setId(IdWorker.getId());
        int i = orderMapper.insertSelective(userOrder);
        Map<String, Object> map = new HashMap<>();
        if(i > 0){
            return Result.buildSuccessResult("order create success");
        }
        else{
            return Result.buildSuccessResult("order create failed");
        }
    }

    public List<UserOrder> selectList(Long userId, Long productId) {
        UserOrderExample example = new UserOrderExample();
        example.createCriteria().andUserIdEqualTo(userId).andProductIdEqualTo(productId).andDeletedEqualTo(0);
        return orderMapper.selectByExample(example);
    }
}
